package compute;

import java.io.*;

public class Compute {
	public static void main(String args[]){
		int i,a,x=0;
		FileInputStream fin=null;
		if(args.length!=1){
			System.out.println("Please enter a correct filename.");
			return;
		}
		try{
			fin=new FileInputStream(args[0]);
			i=fin.read();
			StringBuffer[] word=new StringBuffer[2000];
			int num[]=new int[2000];
			for(int n=0;n<2000;n++)
				num[n]=1;
			StringBuffer s=new StringBuffer("");
			do{
				if(Character.isLetter(i)){
					x++;
					while(Character.isLetter(i)){
						s=s.append((char)i);
						i=fin.read();
					}
					if(x==1)word[0]=s;
					else
					for(int m=x-2;m>=0;m--){
						if(word[m].toString().equals(s.toString())){
							num[m]++;
                                                        x--;
							break;
						}
						if(m==0)word[x-1]=s;
					}
					s=new StringBuffer("");
				}
				else{
					do{
						i=fin.read();
					}while(!(Character.isLetter(i)));
				}
			}while(i!=-1);
                        int n=0;
                        for(i=0;i<x;i++){n+=num[i];}
			System.out.println("This article contains "+n+"words.");
			System.out.println();
			for(i=0;i<x;i++){
				for(int j=i+1;j<x;j++){
					if(num[j]<num[i]){
						a=num[i];
						num[i]=num[j];
						num[j]=a;
						s=word[i];
						word[i]=word[j];
						word[j]=s;
					}
				}
				System.out.println(word[i]+"   sum: "+num[i]);
			}
                }catch(FileNotFoundException e){
			System.out.println("File Not Found.");
		}catch(IOException e){
			System.out.println("An I/O Error Occurred");
		}finally{
			try{
				if(fin!=null)fin.close();
			}catch(IOException e){
				System.out.println("Error Closing File");
			}
		}
	}
}





